package com.quan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.admin.entity.SysRolePermission;
import com.quan.admin.mapper.SysRolePermissionMapper;
import com.quan.admin.service.SysRolePermissionService;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


@Service
public class SysRolePermissionServiceImpl extends MyBaseServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService {

    @Autowired
	private SysRolePermissionMapper mapper;

	@Override
    public IPage<SysRolePermission> selectMyPage(PageUtils pageUtils, SysRolePermission entity) {
        Page<SysRolePermission> page = new Page<SysRolePermission>(pageUtils.getPage(), pageUtils.getLimit());

        // 排序
        super.handleSort(page, pageUtils);

        return this.mapper.selectMyPage(page, entity);
    }

    @Override
    public void deleteByRoleId(Serializable roleId) {
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<SysRolePermission>();
        queryWrapper.eq("role_id", roleId);

        super.remove(queryWrapper) ;
    }

    @Override
    public void deleteByMenuId(Serializable menuId) {
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<SysRolePermission>();
        queryWrapper.eq("menu_id", menuId);
        super.remove(queryWrapper) ;
    }

    @Override
    public void saveRolePermission(List<SysRolePermission> permissions) {
	    super.saveBatch(permissions);
    }

    @Override
    public List<SysRolePermission> getPermissionByRoleId(Long roleId) {
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<SysRolePermission>();
        queryWrapper.eq("role_id", roleId).select("permission_id", "menu_id");
        return super.list(queryWrapper);
    }

    @Override
    public List<SysRolePermission> getPermissionByRoleIds(List<Long> roleIds) {
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<SysRolePermission>();
        queryWrapper.in("role_id", roleIds).select("permission_id", "menu_id");
        return super.list(queryWrapper);
    }

}